// upLoad.js
var app = getApp()
var upload=''
import {uploadFileList} from '../../utils/camera.js';
import { showTi, showToast, showLoading } from "../../utils/modal.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    uploadList: [],
    imgList: [],
    id:null,
    types:null,
    installData:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let self=this
    self.setData({
      id: options.orderId,
      types: options.type
    })
    upload = getCurrentPages()[2]
    console.log(this.data.id, this.data.types, this.data.installData)
  },
  onUnload: function () {
    upload.orderDetailState()
  },
  //上传图纸
  uploadImg: function (e) {
    let self = this;
    let type = e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        self.data.imgList.push(res.tempFilePaths[0])
        self.setData({
          uploadList: self.data.imgList
        })
        console.log(self.data.uploadList)
      }
    });
  },
  //提交
  uploadButton: function () {
    let self = this
    let args = { 'name': 'image', 'type': { 'image_type':this.data.types, 'id': self.data.id } }
    console.log(self.data.uploadList,args)
    uploadFileList(self.data.uploadList, args).then(data => {
      console.log('data', data)
      if (!data.all_success) {
        if (this.data.types =='measure_pictures'){
          this.assign()  
        }else{
          showToast("上传成功")
          app.goBack()
        }     
      }
    })
  },
  //确认指派
  assign: function () {
    let self = this
    let assignObj = wx.getStorageSync('facilitator')
    this.setData({
      installData: assignObj
    }) 
    console.log(self.data.installData)
    let assignData = {
      id: self.data.installData.id,
      name: self.data.installData.real_name,
      tel: self.data.installData.tel,
      facilitator_id: self.data.installData.id,
    }
    console.log(assignData)
    if (self.data.installData) {
      app.query("web/orders/" + self.data.id, assignData, 'put').then(res => {
        if (res.code === 200) {
          showToast("指派成功")
          app.goBack()
        }
      })
    } else {
      showTi('请指派安装师傅');
    }
  },
  //指派安装师傅
  saveCard:function(){
    wx.navigateTo({
      url: '../assignInstall/assignInstall?orderId=' + this.data.id
    })
  },
  //获取师傅
  installObj:function(){
    let assignObj = wx.getStorageSync('facilitator')
    this.setData({
      installData: assignObj
    })  
  }
})